<?php defined( '_VALID_MOS' ) or die( include("404.php") );
switch($_POST["hidden"])
{
	case "";
	// khoi dau trang khong co gia tri submit. khong lam zi ca
	break;
	
	/* khoi su ly su kien submit form them nhom logo */
	case "submit_com_gallery_folder_add";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_addfoldergallery($_POST["title"], $_POST["image_file"],$_POST["description"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("group_gallery", "order")) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_addfoldergallery($_POST["title"], $_POST["image_file"],$_POST["description"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("group_gallery", "order")) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=folder&task=add");
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form them hinh anh vo album */
	case "submit_com_gallery_detail_add";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_adddetailgallery($_POST["gallery_id"], $_POST["title"],$_POST["description"],$_POST["width"],$_POST["height"], $_POST["image_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("gallery", "order")) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=". $_POST["group_type_id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_adddetailgallery($_POST["gallery_id"], $_POST["title"],$_POST["description"],$_POST["width"],$_POST["height"], $_POST["image_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("gallery", "order")) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=detail&task=add&id=". $_POST["group_type_id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=". $_POST["group_type_id"]);
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form thu muc album anh */	
	case "submit_com_gallery_folder_view";
		if($_POST["task"] == "unpublish"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_pulish_and_un_publish_folder("0", $values[$row]);
			}
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
		}
		else if($_POST["task"] == "publish"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_pulish_and_un_publish_folder("1", $values[$row]);
			}
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
		}
		else if($_POST["task"] == "orderup"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_orderdownfolder($values[$row]);				
			}
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
		}
		else if($_POST["task"] == "orderdown"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_orderupfolder($values[$row]);			
			}
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
		}
		else if($_POST["task"] == "remove"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_remove_folder($values[$row]);			
			}
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
		}
	break;
	
	/* khoi su ly su kien submit form hinh anh trong album */	
	case "submit_com_gallery_detail_view";
		if($_POST["task"] == "unpublish"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_pulish_and_un_publish_detail("0", $values[$row]);
			}
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=".$_POST["group_type_id"]);
		}
		else if($_POST["task"] == "publish"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_pulish_and_un_publish_detail("1", $values[$row]);
			}
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=".$_POST["group_type_id"]);
		}
		else if($_POST["task"] == "orderup"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_orderdowndetail($values[$row]);				
			}
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=".$_POST["group_type_id"]);
		}
		else if($_POST["task"] == "orderdown"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_orderupdetail($values[$row]);			
			}
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=".$_POST["group_type_id"]);
		}
		else if($_POST["task"] == "remove"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_remove_detail($values[$row]);			
			}
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=".$_POST["group_type_id"]);
		}
	break;
	
	/* khoi su ly su kien submit form sua thu muc album */
	case "submit_com_gallery_folder_edit";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_editfolder($_POST["title"], $_POST["image_file"], $_POST["description"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_editfolder($_POST["title"], $_POST["image_file"], $_POST["description"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=folder&task=edit&id=".$_POST["Id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_gallery&view=folder&task=view");
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form sua thu muc album */
	case "submit_com_gallery_detail_edit";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_editdetail($_POST["gallery_id"], $_POST["title"], $_POST["description"], $_POST["width"], $_POST["height"], $_POST["image_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=". $_POST["gallery_id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_editdetail($_POST["gallery_id"], $_POST["title"], $_POST["description"], $_POST["width"], $_POST["height"], $_POST["image_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_gallery&view=detail&task=edit&id=".$_POST["Id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi xảy ra, vui lòng làm lại";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_gallery&view=detail&task=view&id=".$_POST["gallery_id"]);
			exit();
		}
	break;
	
	default:
		$core_class->_redirect(".");exit();
	break;
}

class process{

	// ham su ly di chuyen mau tin xuong phia duoi cua folder
	function process_orderdownfolder($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order` from group_gallery WHERE Id = $psesid) As `currenOrder`, 
		(SELECT `order` from group_gallery WHERE `order` > 
		(SELECT `order` from group_gallery WHERE Id = $psesid) 
		Order by `order` LIMIT 1) As `preOrder`,
		(SELECT Id from group_gallery WHERE `order` = 
		(SELECT `order` from group_gallery WHERE `order` > 
		(SELECT `order` from group_gallery WHERE Id = $psesid) 
		Order by `order` LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update group_gallery set `order` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update group_gallery set `order` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}

	// ham su ly di chuyen mau tin len phia tren cua folder
	function process_orderupfolder($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order` from group_gallery WHERE `Id` = $psesid) As `currenOrder`, 
		(SELECT `order` from group_gallery WHERE `order` < 
		(SELECT `order` from group_gallery WHERE `Id` = $psesid) 
		Order by `order` desc LIMIT 1) As `preOrder`,
		(SELECT `Id` from group_gallery WHERE `order` = 
		(SELECT `order` from group_gallery WHERE `order` < 
		(SELECT `order` from group_gallery WHERE `Id` = $psesid) 
		Order by `order` desc LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update group_gallery set `order` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update group_gallery set `order` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly di chuyen mau tin xuong phia duoi cua hinh anh trong album
	function process_orderdowndetail($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order` from gallery WHERE Id = $psesid) As `currenOrder`, 
		(SELECT `order` from gallery WHERE `order` > 
		(SELECT `order` from gallery WHERE Id = $psesid) 
		Order by `order` LIMIT 1) As `preOrder`,
		(SELECT Id from gallery WHERE `order` = 
		(SELECT `order` from gallery WHERE `order` > 
		(SELECT `order` from gallery WHERE Id = $psesid) 
		Order by `order` LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update gallery set `order` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update gallery set `order` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly di chuyen mau tin len phia tren cua folder
	function process_orderupdetail($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order` from gallery WHERE `Id` = $psesid) As `currenOrder`, 
		(SELECT `order` from gallery WHERE `order` < 
		(SELECT `order` from gallery WHERE `Id` = $psesid) 
		Order by `order` desc LIMIT 1) As `preOrder`,
		(SELECT `Id` from gallery WHERE `order` = 
		(SELECT `order` from gallery WHERE `order` < 
		(SELECT `order` from gallery WHERE `Id` = $psesid) 
		Order by `order` desc LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update gallery set `order` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update gallery set `order` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly them thu muc album anh
	function process_addfoldergallery($title, $folder_icon, $description, $date_add, $status, $order){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into group_gallery (`title`, `folder_icon`, `description`, `date_add`, `status`, `order`) VALUES (?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssss", $title, $folder_icon, $description, $date_add, $status, $order);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	
	// ham su ly them hianh vo album
	function process_adddetailgallery($gallery_type_id, $title, $description, $width, $height, $image_file, $date_add, $status, $order){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into gallery (`gallery_type_id`, `title`, `description`, `width`, `height`, `image_file`, `date_add`, `status`, `order`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("sssssssss", $gallery_type_id, $title, $description, $width, $height, $image_file, $date_add, $status, $order);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	
	// ham su lay so thu tu lon nhat cho moi mau tin
	function process_getmaxid($table, $column){
		include("../protected/dbconnect.php");
		$sql = "select MAX(`$column`)+1 As `MaxId` from `$table`;";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($MaxId);
		if($cmd->fetch()){
			if($MaxId == 0)	return 1;
			else return $MaxId;
		}
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly su kien publish va khong publish album
	function process_pulish_and_un_publish_folder($check, $values){
		include("../protected/dbconnect.php");
		if($check == 0)
		$sql = "Update group_gallery Set `status` = 0 Where `Id` = ?";
		else $sql = "Update group_gallery Set `status` = 1 Where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly su kien publish va khong publish hinh anh trong album
	function process_pulish_and_un_publish_detail($check, $values){
		include("../protected/dbconnect.php");
		if($check == 0)
		$sql = "Update gallery Set `status` = 0 Where `Id` = ?";
		else $sql = "Update gallery Set `status` = 1 Where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly go bo album
	function process_remove_folder($values){
		include("../protected/dbconnect.php");
		$myprocess = new process;
		$sql = "Delete from `group_gallery` where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly go bo hianh anh trong album
	function process_remove_detail($values){
		include("../protected/dbconnect.php");
		$myprocess = new process;
		$sql = "Delete from `gallery` where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua thu muc album
	function process_editfolder($title, $folder_icon, $description, $date_add, $activated, $Id){
		include("../protected/dbconnect.php");
		$sql = "UPDATE group_gallery SET `title` = ?, `folder_icon` = ?, `description` = ?, `date_add` = ?, `status` = ? WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssss", $title, $folder_icon, $description, $date_add, $activated, $Id);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua anh trong allbum
	function process_editdetail($gallery_type_id, $title, $description, $width, $height, $image_file, $date_add, $activated, $Id){
		include("../protected/dbconnect.php");
		$sql = "UPDATE gallery SET `gallery_type_id` = ?, `title` = ?, `description` = ?, `width` = ?, `height` = ?, `image_file` = ?, `date_add` = ?, `status` = ? WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("sssssssss", $gallery_type_id, $title, $description, $width, $height, $image_file, $date_add, $activated, $Id);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
}